package lol.clann.minecraft.springboot.api.model.reflect;

import com.ruoshui.utils.java.exception.ExceptionUtils;
import lol.clann.minecraft.springboot.api.common.Invoker;
import lombok.Getter;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 *
 * @author pyz
 * @date 2019/5/2 10:30 PM
 */
public class ConstructorInvoker implements Invoker {
    @Getter
    private final Constructor constructor;

    public ConstructorInvoker(Constructor constructor) {
        this.constructor = constructor;
        constructor.setAccessible(true);
    }

    public Object newInstance(Object... params) {
        try {
            return constructor.newInstance(params);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            ExceptionUtils.throwException(e);
            return null;
        }
    }

    @Override
    public Object invoke(Object target, Object... params) {
        return newInstance(params);
    }
}
